#Note to run the animation you will have to instal Imagemagik from http://www.imagemagick.org/script/binary-releases.php
# when you install Imagemagik remember to select the "instal legacy files" option so convert.exe is included

#Set working directory where this script and the raw excel file are saved 
setwd("C:/Users/Dan/Documents/GitHub/MelbournePropertyPrices")

### Install/load required packages
#List of R packages required for this analysis:
required_packages <- c("ggmap", "readr", "ggplot2", "dplyr", "readxl","tidyr",
                       "stringr", "gganimate","animation","ggiraph")
#Install required_packages:
new.packages <- required_packages[!(required_packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
#Load required_packages:
lapply(required_packages, require, character.only = TRUE)
## Loading required package: ggmap
## Loading required package: ggplot2
## Loading required package: readr
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## Loading required package: readxl
## Loading required package: tidyr
## Loading required package: stringr
## Loading required package: gganimate
## Loading required package: animation
## Loading required package: ggiraph
## [[1]]
## [1] TRUE
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] TRUE
## 
## [[5]]
## [1] TRUE
## 
## [[6]]
## [1] TRUE
## 
## [[7]]
## [1] TRUE
## 
## [[8]]
## [1] TRUE
## 
## [[9]]
## [1] TRUE
## 
## [[10]]
## [1] TRUE
#Set decimal points and disable scientific notation
options(digits=3, scipen=999) 

###Load Map
# Can download using get_map function:
# Melbourne <- get_map(location = 'Melbourne, Australia', zoom = 10, maptype="terrain")
# But I saved it to workind directory so can load it and work offline:
load("Melbourne.rda")

###Load Apartment price data
# Got this data here http://www.dtpli.vic.gov.au/property-and-land-titles/property-information/property-prices ...
# ...but saved it to workind directory so can load it and work offline
suburb_Apartment_2015<-read_excel("suburb_unit_2015.xls", col_names=F, skip = 3) %>% 
            na.omit() %>%
            distinct() %>%
            mutate(`Property Type`="Apartment")
#Load House Price Data and row bind it to Apartment price data 
property_2005_2015<-read_excel("suburb_house_2015.xls", col_names=F, skip = 3) %>% 
            na.omit() %>%
            distinct() %>%
            mutate(`Property Type`="House") %>%
            bind_rows(suburb_Apartment_2015)
rm(suburb_Apartment_2015)


#rename the columns 
names(property_2005_2015)<-c("suburb", 2005:2015, "Prelim 2016", "Change 2014-2015 (%)", "Change 2005-2015 (%)", "Growth (%) PA 2005-2015","Property Type")

# assuming that "-" and "0" means data not available (NA), 
property_2005_2015[property_2005_2015 == 0] <- NA
property_2005_2015[property_2005_2015 == "-"] <- NA


## Read in the lat/long data:
# Also saved it to workind directory so can load it and work offline:
lat_long<-read_csv("Australian_Post_Codes_Lat_Lon.csv")  %>% 
    mutate(postcode=as.character(postcode)) %>%
    distinct() %>%
    select(-dc, -type)
## Parsed with column specification:
## cols(
##   postcode = col_integer(),
##   suburb = col_character(),
##   state = col_character(),
##   dc = col_character(),
##   type = col_character(),
##   lat = col_double(),
##   lon = col_double()
## )
#create VIC only lat_long
VIC_lat_long<- lat_long %>% 
    filter(state=="VIC") %>%
    select(-postcode) %>%
    distinct() %>% 
    filter(suburb %in%  property_2005_2015$suburb)

#Merge VIC_lat_long into property_2005_2015
property_2005_2015<- full_join(property_2005_2015, VIC_lat_long,  by=c("suburb"))
rm(VIC_lat_long)


property_2005_2015 <- property_2005_2015 %>%
    mutate(`Change 2014-2015 (%)` = as.numeric(`Change 2014-2015 (%)`),
           `Change 2005-2015 (%)` = as.numeric(`Change 2005-2015 (%)`),
           `Growth (%) PA 2005-2015` = as.numeric(`Growth (%) PA 2005-2015`))
    

#melt from wide to long format
property_2005_2015<-property_2005_2015 %>% 
    gather(key=Year, value=`Median Price ($)`, -suburb, -lon, -lat, -state, -`Property Type`, -`Change 2014-2015 (%)`, -`Change 2005-2015 (%)`, -`Growth (%) PA 2005-2015`) %>%
    mutate(`Median Price ($)`=as.numeric(`Median Price ($)`))

Map

p1<-ggmap(Melbourne) + 
    geom_point(data = property_2005_2015, 
               aes(x =lon, y= lat, frame = Year, size=`Median Price ($)`, 
                   colour = `Median Price ($)`), alpha=.75, shape="$") +
        scale_colour_gradientn(colours=rainbow(5)) +
        scale_radius (range = c(4, 14), trans = "identity", guide = "legend") +
    facet_wrap(~`Property Type`) +
    ggtitle("Median Melbourne Property Prices ($) from 2005-2016 \n")

p1 <- p1 + theme(aspect.ratio=1) +
        theme(axis.title.x = element_blank(), 
            axis.text.x  =  element_blank(),
            axis.title.x = element_blank(),
            axis.ticks.x=element_blank(),
            axis.text.y  =  element_blank(), 
            axis.title.y = element_blank(),
            axis.ticks.y=element_blank(),
            legend.title = element_text(size=12, face="bold"),
            legend.text = element_text(size = 12, face = "bold"),
            strip.text.x = element_text(size=12, face="bold"),
            plot.title = element_text(size = 14, face = "bold"),
            legend.position="right")  

#Save the .gif to working directory
# gg_animate(p1, 'Melbourne.gif', ani.width = 1000, ani.height = 600, interval = 0.75)

#show the gif in the graphics pannel
# gg_animate(p1)

Change Map

p2<-ggmap(Melbourne, maprange=TRUE, extent = "normal", base_layer = ggplot(data = property_2005_2015, aes(x =lon, y= lat))) + 
    geom_point_interactive(data = property_2005_2015, 
                           aes(x =lon, y= lat, size= `Change 2005-2015 (%)`, 
                               colour = `Change 2005-2015 (%)`, tooltip=suburb, data_id = suburb),
                           alpha=.5) +
    scale_colour_gradientn(colours=rainbow(5)) +
    scale_radius (range = c(1, 6), trans = "identity", guide = "legend") +
    facet_wrap(~`Property Type`) +
    ggtitle("Change in Median Melbourne \n Property Prices ($) from 2005-2015 \n") +
    theme(axis.title.x = element_blank(), 
        axis.text.x  =  element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y  =  element_blank(), 
        axis.title.y = element_blank(),
        axis.ticks.y=element_blank(),
        legend.title = element_text(face="bold"),
        legend.text = element_text(face = "bold"),
        strip.text.x = element_text(face="bold"),
        plot.title = element_text(face = "bold"))  


ggiraph(code = {print(p2)}, zoom_max = 5,
        tooltip_offx = 20, tooltip_offy = -10, 
        hover_css = "fill:black;",
        tooltip_opacity = 0.7)
# ggmap(Melbourne) + 
#     geom_point(data = property_2005_2015, 
#                aes(x =lon, y= lat, size=`Change ($)`, 
#                    colour = `Change ($)`), alpha=.75, shape="$") +
#         scale_colour_gradientn(colours=rainbow(5)) +
#         scale_radius (range = c(1, 8), trans = "identity", guide = "legend") +
#     facet_wrap(~`Property Type`) +
#     ggtitle("Change in Median Melbourne Property Prices ($) from 2005-2015 \n")



p3<-ggmap(Melbourne) + 
    geom_point(data = property_2005_2015, 
               aes(x =lon, y= lat, size=`Change 2005-2015 (%)`, 
                   colour = `Change 2005-2015 (%)`), alpha=.5, shape="%") +
        scale_colour_gradientn(colours=rainbow(5)) +
        scale_radius (range = c(1, 6), trans = "identity", guide = "legend") +
    facet_wrap(~`Property Type`) +
    ggtitle("% Change in Median Melbourne \n Property Prices ($) from 2005-2016 \n")

p3 <- p3 + theme(aspect.ratio=1) +
        theme(axis.title.x = element_blank(), 
            axis.text.x  =  element_blank(),
            axis.title.x = element_blank(),
            axis.ticks.x=element_blank(),
            axis.text.y  =  element_blank(), 
            axis.title.y = element_blank(),
            axis.ticks.y=element_blank(),
            legend.title = element_text(size=12, face="bold"),
            legend.text = element_text(size = 12, face = "bold"),
            strip.text.x = element_text(size=12, face="bold"),
            plot.title = element_text(size = 14, face = "bold"),
            legend.position="right") 
p3

p4<-ggmap(Melbourne) + 
    geom_point(data = property_2005_2015, 
               aes(x =lon, y= lat, size=`Growth (%) PA 2005-2015`, 
                   colour = `Growth (%) PA 2005-2015`), alpha=.5) +
        scale_colour_gradientn(colours=rainbow(5)) +
        scale_radius (range = c(1, 6), trans = "identity", guide = "legend") +
    facet_wrap(~`Property Type`) +
    ggtitle("Growth (%) per annum in Median Melbourne \n Property Prices ($) from 2005-2016 \n")

p4 <- p4 + theme(aspect.ratio=1) +
        theme(axis.title.x = element_blank(), 
            axis.text.x  =  element_blank(),
            axis.title.x = element_blank(),
            axis.ticks.x=element_blank(),
            axis.text.y  =  element_blank(), 
            axis.title.y = element_blank(),
            axis.ticks.y=element_blank(),
            legend.title = element_text(size=12, face="bold"),
            legend.text = element_text(size = 12, face = "bold"),
            strip.text.x = element_text(size=12, face="bold"),
            plot.title = element_text(size = 14, face = "bold"),
            legend.position="right") 
p4